package com.shaw.mybatisx.session;

import com.shaw.mybatisx.proxy.MapperProxy;
import com.shaw.mybatisx.executor.Executor;

import java.lang.reflect.Proxy;

/**
 * Created by asus on 2018/4/1.
 */
public class SqlSession {
    private Configuration configuration;
    private Executor executor;

    public SqlSession(Configuration configuration) {
        this.configuration = configuration;
        executor = configuration.newExecutor();
    }

    public <E> Object getMapper(Class<E> clazz){
        return (E)Proxy.newProxyInstance(clazz.getClassLoader(),new Class[]{clazz},new MapperProxy(clazz,this));
    }

    public <E> E selectOne(Configuration.MapperDate mapperDate,Object parameter){
        return (E) executor.query(mapperDate,parameter).get(0);
    }

    public Configuration getConfiguration() {
        return configuration;
    }

    public Executor getExecutor() {
        return executor;
    }
}
